<template>
  <a-breadcrumb class="breadcrumb">
    <a-breadcrumb-item v-for="(item, index) in breadList" :key="item.name">
      <router-link
        v-if="item.name != name && index != 1"
        :to="{ path: item.path === '' ? '/' : item.path }"
      >{{ item.meta.title }}</router-link>
      <span v-else>{{ item.meta.title }}</span>
    </a-breadcrumb-item>
  </a-breadcrumb>
</template>

<script>
import zhTranslations from '@/lang/zh.json'
import enTranslations from '@/lang/en.json'
export default {
  data () {
    return {
      name: '',
      breadList: [],
      lang: ''
    }
  },
  created () {
    this.changeBreadCrumbLang()
    this.getBreadcrumb()
  },
  mounted () {
    this.changeBreadCrumbLang()
  },
  methods: {
    getBreadcrumb () {
      this.breadList = []
      // this.breadList.push({name: 'index', path: '/dashboard/', meta: {title: '首页'}})
      this.name = this.$route.name
      this.$route.matched.forEach(item => {
        // item.name !== 'index' && this.breadList.push(item)
        this.breadList.push(item)
      })
    },
    changeBreadCrumbLang () {
      this.lang = this.$store.state.lang
      // console.log('bbb', this.lang)
      if (this.lang === 'en') {
        this.breadList.forEach(item => {
          for (const [key, value] of Object.entries(zhTranslations.menu)) {
          // 如果找到对应的中文标题，获取对应的英文翻译标题键
          if (value === item.meta.title) {
            console.log(item.meta.title)
            // 检查英文翻译文件中是否存在对应的英文翻译标题
            if (enTranslations.menu.hasOwnProperty(key)) {
              // 返回对应的英文翻译
               item.meta.title = enTranslations.menu[key]
            }
          }
        }
        })
        // this.lang = 'en'
      } else {
        if (this.lang === 'zh') {
        this.breadList.forEach(item => {
          for (const [key, value] of Object.entries(enTranslations.menu)) {
          // 如果找到对应的英文标题，获取对应的中文翻译标题键
          if (value === item.meta.title) {
            // 检查中文翻译文件中是否存在对应的中文翻译标题
            if (zhTranslations.menu.hasOwnProperty(key)) {
              // 返回对应的中文翻译
               item.meta.title = zhTranslations.menu[key]
               console.log('中文', item.meta.title)
            }
          }
        }
        })
        // this.lang = 'zh'
      }
    }
  }
  },
  computed: {
    status() {
      return this.$store.state.isEnglish
    }
  },
  watch: {
    status() {
      this.changeBreadCrumbLang()
    },
    $route () {
      this.getBreadcrumb()
    }
  }
}
</script>

<style scoped>
</style>
